package com.esun.service.mq.rabbitmq.listener;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.esun.dao.das.OperationLogDas;
import com.esun.dao.eo.OperationLogEo;
import com.esun.service.component.OperationLogComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;

/**
 * @author John.xiao
 * @date 2021/10/12 17:41
 */
@Component
@RabbitListener(queues = "operationLog")
@Slf4j
public class OperationLogListener {
	@Resource
	OperationLogDas operationLogDas;
	@Resource
	OperationLogComponent operationLogComponent;

	@RabbitHandler
	public void process(String message){
		log.info("接收到操作日志:{}",JSONObject.parseObject(message, OperationLogEo.class));
		try {
			OperationLogEo databaseLog  = JSONObject.parseObject(message,OperationLogEo.class);
			operationLogDas.insert(databaseLog);
		}
		catch (Exception e){
			log.info("消费消息出现错误,记录到错误缓存中:{}"+JSONObject.toJSONString(message, SerializerFeature.PrettyFormat));
			e.printStackTrace();
			Date now = new Date();
			Long time = now.getTime();
			operationLogComponent.setErrorLogs(time,message);
		}
	}
}
